package com.hourse.platform.controller;

import com.hourse.platform.vo.MenuButtonVO;
import com.hourse.platform.vo.MenuVO;
import com.hourse.platform.vo.PageData;
import com.hourse.platform.vo.ResultMsg;
import com.hourse.platform.entity.MenuButtonEntity;
import com.hourse.platform.entity.MenuEntity;
import com.hourse.platform.service.MenuService;
import com.hourse.platform.utils.StringTools;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "系统-菜单服务")
@RestController
@RequestMapping("platform/menu")
@Slf4j
public class MenuController {

    @Autowired
    private MenuService menuService;

    /**
     * @param menuVO 1
     * @return : com.hourse.platform.vo.Message
     * @功能描述: 【创建菜单】
     */
    @ApiOperation(value = "系统-创建菜单", response = MenuVO.class)
    @SneakyThrows
    @PostMapping(value = "saveMenu")
    public PageData saveMenu(@RequestBody MenuVO menuVO) {

        MenuEntity menuEntity =
                (MenuEntity) menuVO.bindEnity(MenuEntity.class);

        if (StringTools.isNull(menuVO.getParentId())) {
            menuEntity.setParentId(null);
        }

        menuService.saveOrUpdate(menuEntity);
        menuVO.setId(menuEntity.getId());

        return menuVO.SUCCESS();
    }

    /**
     * @return : com.hourse.platform.vo.Message
     * @功能描述: 【获取菜单列表】
     */
    @ApiOperation(value = "系统-菜单列表")
    @SneakyThrows
    @GetMapping(value = "menuList")
    public ResultMsg<List<MenuVO>> menuList(@RequestParam(required = false) String menuId) {

        return new ResultMsg(menuService.getMenuList(menuId));
    }

    /**
     * @功能描述: 【创建或修改菜单按钮】
     */
    @ApiOperation(value = "系统-创建菜单按钮")
    @SneakyThrows
    @PostMapping(value = "saveMenuButton")
    public PageData saveMenuButton(@RequestBody MenuButtonVO menuButtonVO) {

        MenuButtonEntity menuButtonEntity =
                (MenuButtonEntity) menuButtonVO.bindEnity(MenuButtonEntity.class);

        menuService.saveOrUpdate(menuButtonEntity);
        menuButtonVO.setId(menuButtonEntity.getId());

        return menuButtonVO.SUCCESS();
    }

    /**
     * @功能描述: 【获取菜单按钮】
     */
    @ApiOperation(value = "系统-菜单按钮列表")
    @GetMapping(value = "menuButtonList/{menuId}")
    public ResultMsg<List<MenuButtonEntity>> menuButtonList(@PathVariable String menuId) {

        return new ResultMsg(menuService.getMenuButtonList(menuId));
    }

    /**
     * @功能描述: 【删除菜单按钮】
     * @作 者 : Rain
     * @创建时间 : 2020/12/9 17:43
     */
    @SneakyThrows
    @PostMapping(value = "deleteMenuButton")
    public PageData deleteMenuButton(@RequestBody MenuButtonVO menuButtonVO) {
        menuService.deleteMenuButton(menuButtonVO.getId());
        return menuButtonVO.SUCCESS();
    }


    /**
     * @功能描述: 【删除菜单】
     */
    @SneakyThrows
    @PostMapping(value = "deleteMenu")
    public PageData deleteMenu(@RequestBody MenuVO menuVO) {


        menuService.deleteMenu(menuVO.getId());
        return menuVO.SUCCESS();
    }
}
